package com.zy.algorithm.v3.sort;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.LinearLayout;

import com.zy.activity.BaseFragment;
import com.zy.algorithm.v3.R;
import com.zy.utils.DataBuildUtils;
import com.zy.utils.data.Point;
import com.zy.view.sort.DIYSortView;

import java.util.ArrayList;
import java.util.List;


//堆排序
public class HeapSortFragment extends BaseFragment {

    @Override
    protected int getLayout() {
        return R.layout.fragment_sort_merge;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        DIYSortView diy_v4 = view.findViewById(R.id.sort_view);
        View click_black = view.findViewById(R.id.click_black);
        click_black.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                autoMove(diy_v4);
                return false;
            }
        });
        int[] array = new int[]{
                10, 1, 2, 5, 14, 13, 11, 9, 6, 12, 15, 3, 7, 8, 4
        };


        //DataBuildUtils.getRandomSortArrayV2(array);

        diy_v4.setD(120);
        diy_v4.create(2, 1, array, Point.Type.ROUND_INDEX);
        //diy_v4.addAss(d, 2, 1, "V", per);

    }

    //自动动画

    boolean hasMove = false;

    private void autoMove(DIYSortView diy_v4) {
        if (hasMove) {
            addAssistLineList(diy_v4);
            return;
        }
        hasMove = true;
        List<DIYSortView.AutoModeStep> list = new ArrayList<>();
        int baseX = 10;
        list.add(new DIYSortView.AutoModeStep(0, baseX, 2));
        list.add(new DIYSortView.AutoModeStep(1, baseX - 4, 3));
        list.add(new DIYSortView.AutoModeStep(2, baseX + 4, 3));

        list.add(new DIYSortView.AutoModeStep(3, baseX - 6, 4));
        list.add(new DIYSortView.AutoModeStep(4, baseX - 2, 4));
        list.add(new DIYSortView.AutoModeStep(5, baseX + 2, 4));
        list.add(new DIYSortView.AutoModeStep(6, baseX + 6, 4));


        int depth4 = 6;
        list.add(new DIYSortView.AutoModeStep(7, baseX - 7, depth4));
        list.add(new DIYSortView.AutoModeStep(8, baseX - 5, depth4));
        list.add(new DIYSortView.AutoModeStep(9, baseX - 3, depth4));
        list.add(new DIYSortView.AutoModeStep(10, baseX - 1, depth4));
        list.add(new DIYSortView.AutoModeStep(11, baseX + 1, depth4));
        list.add(new DIYSortView.AutoModeStep(12, baseX + 3, depth4));
        list.add(new DIYSortView.AutoModeStep(13, baseX + 5, depth4));
        list.add(new DIYSortView.AutoModeStep(14, baseX + 7, depth4));


        diy_v4.autoMove(list);
    }

    boolean hasAddLine = false;

    private void addAssistLineList(DIYSortView diy_v4) {
        if (hasAddLine) {
            return;
        }
        hasAddLine = true;
        List<DIYSortView.NodeLineStep> list = new ArrayList<>();

        for (int i = 0; i <= 6; i++) {
            list.add(new DIYSortView.NodeLineStep(i, i * 2 + 1));
            list.add(new DIYSortView.NodeLineStep(i, i * 2 + 2));
        }


        diy_v4.autoAddAssistLine(list);
    }
}
